<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    
    <form id="DataForm" action="" method="POST" class="layui-form layui-form-pane">
        <div class="layui-tab layui-tab-brief" lay-filter="itemTab">

            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">

                  
                    <div class="layui-form-item">
                        <label class="layui-form-label">单选框</label>
                        <div class="layui-input-block">
                            <input type="radio" name="info[type]" lay-filter="type" value="1" title="无门槛" {if $info['type']==1}checked{/if}>
                            <input type="radio" name="info[type]" lay-filter="type" value="2" title="满减"  {if $info['type']==2}checked{/if}>
                            <input type="radio" name="info[type]"  lay-filter="type"value="3" title="指定商品"  {if $info['type']==3}checked{/if}>
                        </div>
                    </div>

                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">选择经销商</label>
                        <div class="layui-input-block">
                            <input type="text" placeholder="选择经销商（不选择自动默认平台发送优惠卷）" autocomplete="off" class="layui-input"
                            id="user_select" value="{$info['bus_username']}">
                        <input type="hidden" autocomplete="off" class="layui-input" id="user_id"
                            name="info[user_id]" value="{$info['user_id']}">
                        </div>
                    </div>

                    <div class="layui-form-item" id="type_3">
                        <label class="layui-form-label">选择商品</label>
                        <div class="layui-input-block">
                            <input type="text" placeholder="选择商品" autocomplete="off" class="layui-input"
                            id="goods_select" value="{$info['goods_title']}">
                        <input type="hidden" autocomplete="off" class="layui-input type_3" id="goods_id"
                            name="info[goods_id]" value="{$info['goods_id']}">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="info[title]" placeholder="请输入标题" autocomplete="off"
                                class="layui-input" value="{$info['title']}">
                        </div>
                    </div>
  
                    <div class="layui-form-item" id="type_1">
                        <label class="layui-form-label">金额</label>
                        <div class="layui-input-block">
                            <input type="number" name="info[money]" placeholder="请输入金额" autocomplete="off"
                                class="layui-input type_1" value="{$info['money']}">
                        </div>
                    </div>

                    <div class="layui-form-item" id="type_2">
                        <label class="layui-form-label">满金额</label>
                        <div class="layui-input-inline">
                            <input type="number" name="info[full_money]" placeholder="请输入金额" autocomplete="off"
                                class="layui-input type_2" value="{$info['full_money']}">
                        </div>
                        <label class="layui-form-label">减金额</label>
                        <div class="layui-input-inline">
                            <input type="number" name="info[money]" placeholder="请输入金额" autocomplete="off"
                                class="layui-input type_2" value="{$info['money']}" disabled>
                        </div>
                    </div>
                    

                    <div class="layui-form-item">
                        <label class="layui-form-label">有效期</label>
                        <div class="layui-input-block">
                            <input type="number" name="info[limittime]" placeholder="请输入有效期" autocomplete="off"
                                class="layui-input" value="{$info['limittime']}">
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">排序</label>
                        <div class="layui-input-inline" >
                            <input type="number" name="info[sort]" placeholder="请输入排序" autocomplete="off"
                                class="layui-input" value="{$info['sort']}">
                        </div>
                        <div class="layui-form-mid layui-word-aux">从大到小</div>
                    </div>
                
                  
                    <div class="layui-form-item">
                        <input type="hidden" name="id" value="{$info['id']}">
                        <div class="layui-input-block" style="margin:auto;text-align: center;">
                            <button id="saveButton" type="submit" class="layui-btn"><i class="fa fa-check"></i>
                                提交</button>
                            <button id="closeButton" type="button" class="layui-btn layui-btn-primary"><i
                                    class="fa fa-close"></i>
                                关闭</button>
                        </div>
                    </div>
                </div>

              

            </div>
        </div>
    </form>
</div>
<script src="/static/admin/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/admin/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
      layui.use(['layer', 'form', 'laydate', 'element', 'upload','tableSelect','wangEditor'], function () {
        var    tableSelect = layui.tableSelect;
        var $ = layui.$, layer = layui.layer, table = layui.table,  element = layui.element,laydate = layui.laydate, form = layui.form, upload = layui.upload;
        var indexFrame = parent.layer.getFrameIndex(window.name); //获取窗口索引
            $("#type_1").show();
            $("#type_2").hide();
            $("#type_3").hide();
            $("#saveButton").on('click', function () {
                var loading = layer.open({ type: 3 });
                var data = $("#DataForm").serialize()
                $.ajax({
                    url: "{:url('edit')}",
                    type: 'post',
                    data:data,
                    success: function (data) {
                        console.log(data);
                        layer.close(loading);
                        if (data.code == 0) {
                            parent.tableReload();
                            parent.layer.close(indexFrame);
                            parent.layer.msg(data.msg);
                        } else {
                            layer.msg(data.msg);
                        }
                    }
                })
                return false;
            })
            upload.render({
                    elem: '#upload_image', url: "{:url('common/images')}", done: function (res) {
                            $("#image").val(res.data.name);
                            layer.msg(res.msg);
                    }
            });
            $("#closeButton").on('click', function () {
                parent.layer.close(indexFrame);
            })
            layui.form.on('radio(type)', function(data){
             
                    switch (data.value) {
                        case '1':
                            $("#type_1").show();
                            $("#type_2").hide();
                            $("#type_3").hide();
                            $(".type_1").removeAttr("disabled");
                            $(".type_2").attr("disabled","disabled");
                            $(".type_3").attr("disabled","disabled");
                        break;
                        case '2':
                            $("#type_2").show();
                            $("#type_1").hide();
                            $("#type_3").hide();
                            $(".type_1").attr("disabled","disabled");
                            $(".type_3").attr("disabled","disabled");
                            $(".type_2").removeAttr("disabled");
                        break;
                        case '3':
                            $("#type_1").show();
                            $("#type_2").hide();
                            $("#type_3").show();
                            $(".type_2").attr("disabled","disabled");
                            $(".type_1").removeAttr("disabled");
                            $(".type_3").removeAttr("disabled");
                        break;
                        default:
                            break;
                    }
            });
            tableSelect.render({
                elem: '#user_select',
                checkedKey: 'id',
                searchKey: 'title',
                table: {
                    url: '/admin/bus.user/list',
                    cols: [[
                        { type: 'radio' },
                        { field: 'id', title: 'ID' },
                        { field: 'user_name', title: '名称' },
                    ]]
                },
                done: function (elem, data) {

                    var idArr = []
                    var title = []
                    layui.each(data.data, function (index, item) {
                        title.push(item.user_name)
                        idArr.push(item.id);
                    })
                    $("#user_id").val(idArr.join(","));
                    $("#user_select").val(title.join(","));
                    var type = $("input[type='radio']:checked").val();
                    $("#goods_id").val();
                    $("#goods_select").val();
                    if(type == 3){
                        tableSelect.render({
                            elem: '#goods_select',
                            checkedKey: 'id',
                            searchKey: 'title',
                            table: {
                                where:{"bus_id": $("#user_id").val()},
                                url: '/admin/goods.goods/list',
                                cols: [[
                                    { type: 'radio' },
                                    { field: 'id', title: 'ID' },
                                    { field: 'title', title: '商品' },
                                ]]
                            },
                            done: function (elem, data) {
                                var idArr = []
                                var title = []
                                layui.each(data.data, function (index, item) {
                                    title.push(item.title)
                                    idArr.push(item.id);
                                })
                                $("#goods_id").val(idArr.join(","));
                        
                                $("#goods_select").val(title.join(","));
                            }
                        })
                    }
                  
                }
            })


        });
</script>
</body>
</html>